package com.dingshi.security;

import java.io.ByteArrayInputStream;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Base64InputStream;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.RepairApi;
import com.dingshi.security.db.SettingLoader;
import com.dingshi.security.entity.Detail;
import com.dingshi.security.util.Utils;
import com.dingshi.security.view.LoadDialog;

public class ServicesReceiverActivity extends BaseActivity implements
		OnClickListener {
	protected static final int ID_LOADING = 2300;
	private Button btn_back;
	private TextView title_text;
	private Button btn_right;
	private TextView edit_deparment;
	private TextView edit_xiaoqu;
	private TextView edit_duty_man;
	private TextView edit_phone;
	private TextView edit_shangbao_date;
	private TextView edit_device;
	private TextView edit_break;
	private String oddNum;
	private LinearLayout image_item_top;
	private LinearLayout image_item_bottom;
	private ImageView p1;
	private ImageView p2;
	private ImageView p3;
	private ImageView p4;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.services_track_detail);

		initTitle();
		initComponents();
		registerButton();

		oddNum = getIntent().getStringExtra("oddNum");
		getRepairDetail(oddNum, "0");
	}

	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ID_LOADING:
			LoadDialog dialog = new LoadDialog(this, getResources().getString(
					R.string.login_loading));
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}

	private void getRepairDetail(String oddNum, String contentType) {
		RepairApi api = new RepairApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
				JSONObject jsonEntity = apiResult.getJsonEntity();
				Log.e("aa", jsonEntity.toString());
				if (jsonEntity != null) {
					// {"d":{"dt":"2013-3-22 13:48:56","res":"","rdt":"","repd":null,"d":"双兴监控室",
					//"p4":null,"e":"双兴摄像头5","p3":null,"p2":null,"p1":null,"ri":null,"xq":"双兴小区","rea":"","rd":"故障描述","eid":null,"i":"5","w":"维修人一","__type":"RepairBillDetailReturn:r","red":"诚旭网络监控维修","s":"张三","edt":"","stel":"13322332233","st":"1","rcd":"","rer":""},"t":0}


					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							JSONObject obj = jsonEntity.getJSONObject("d");
							Detail detail = null;
							if (obj != null) {
								detail = new Detail();
								if (obj.has("i")) {
									detail.setI(obj.getString("i"));
								}
								if (obj.has("st")) {
									detail.setSt(obj.getString("st"));
								}
								if (obj.has("d")) {
									detail.setD(obj.getString("d"));
								}
								if (obj.has("xq")) {
									detail.setXq(obj.getString("xq"));
								}
								if (obj.has("s")) {
									detail.setS(obj.getString("s"));
								}
								if (obj.has("stel")) {
									detail.setStel(obj.getString("stel"));
								}
								if (obj.has("dt")) {
									detail.setDt(obj.getString("dt"));
								}
								if (obj.has("e")) {
									detail.setE(obj.getString("e"));
								}
								if (obj.has("rd")) {
									detail.setRd(obj.getString("rd"));
								}
								if (obj.has("p1")) {
									detail.setP1(obj.getString("p1"));
								}
								if (obj.has("p2")) {
									detail.setP2(obj.getString("p2"));
								}
								if (obj.has("p3")) {
									detail.setP3(obj.getString("p3"));
								}
								if (obj.has("p4")) {
									detail.setP4(obj.getString("p4"));
								}

								// 维修信息
								if (obj.has("red")) {
									detail.setRed(obj.getString("red"));
								}
								if (obj.has("rdt")) {
									detail.setRdt(obj.getString("rdt"));
								}
								if (obj.has("rer")) {
									detail.setRer(obj.getString("rer"));
								}
								if (obj.has("rea")) {
									detail.setRea(obj.getString("rea"));
								}
								if (obj.has("w")) {
									detail.setW(obj.getString("w"));
								}
								if (obj.has("edt")) {
									detail.setEdt(obj.getString("edt"));
								}
								if (obj.has("res")) {
									detail.setRes(obj.getString("res"));
								}
								if (obj.has("rcd")) {
									detail.setRcd(obj.getString("rcd"));
								}
							}
							updateUI(detail);
							break;
						case 1:// 数组
							break;
						case -1:// 错误
							dismissDialog(ID_LOADING);
							break;

						default:
							dismissDialog(ID_LOADING);
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(ID_LOADING);
			}
		});
		api.getRepairDetail(1, oddNum, contentType);
	}

	private void updateUI(Detail detail) {
		if (detail != null) {
			edit_deparment.setText(detail.getD());
			edit_shangbao_date.setText(detail.getDt());
			edit_duty_man.setText(detail.getS());
			edit_device.setText(detail.getE());
			edit_break.setText(detail.getRd());
			edit_xiaoqu.setText(detail.getXq());
			edit_phone.setText(detail.getStel());
			// 维修信息
//			edit_unit.setText(detail.getRed());
//			edit_jiebao_date.setText(detail.getRdt());
//			edit_confirm_break.setText(detail.getRer());
//			edit_suggestion.setText(detail.getRea());
//			edit_repair_man.setText(detail.getW());
//			edit_predict_date.setText(detail.getEdt());
//			edit_repair_result.setText(detail.getRes());
			
			if(!Utils.isNullOrEmpty(detail.getP1())||!Utils.isNullOrEmpty(detail.getP2())) {
				image_item_top.setVisibility(View.VISIBLE);
				if(!Utils.isNullOrEmpty(detail.getP1())) {
					displayImage(p1,detail.getP1());
				}
				if(!Utils.isNullOrEmpty(detail.getP2())) {
					displayImage(p2,detail.getP2());
				}
			}
			
			if(!Utils.isNullOrEmpty(detail.getP3())||!Utils.isNullOrEmpty(detail.getP4())) {
				image_item_bottom.setVisibility(View.VISIBLE);
				if(!Utils.isNullOrEmpty(detail.getP3())) {
					displayImage(p3,detail.getP3());
				}
				if(!Utils.isNullOrEmpty(detail.getP4())) {
					displayImage(p4,detail.getP4());
				}
			}
			
		}
	}

	private void displayImage(ImageView image, String data) {
		byte[] buffer = data.getBytes();
		Base64InputStream ips = new Base64InputStream(new ByteArrayInputStream(buffer), Base64.DEFAULT);
		Bitmap bitmap = BitmapFactory.decodeStream(ips);
		if(bitmap!=null) {
			image.setImageBitmap(bitmap);
		}else {
			image.setImageResource(R.drawable.dail_default);
		}
	}


	private void initTitle() {
		btn_back = (Button) findViewById(R.id.btn_back);
		title_text = (TextView) findViewById(R.id.title_text);
		btn_right = (Button) findViewById(R.id.btn_refresh);
		btn_right.setVisibility(View.VISIBLE);
		title_text.setText("派工接收");
		btn_right.setText("确认接收");

	}

	private void registerButton() {
		btn_right.setOnClickListener(this);
		btn_back.setOnClickListener(this);
	}

	private void initComponents() {
		edit_deparment = (TextView) findViewById(R.id.edit_deparment);
		edit_shangbao_date = (TextView) findViewById(R.id.edit_shangbao_date);
		edit_duty_man = (TextView) findViewById(R.id.edit_duty_man);
		edit_device = (TextView) findViewById(R.id.edit_device);
		edit_xiaoqu = (TextView)findViewById(R.id.edit_xiaoqu_date);
		edit_phone = (TextView)findViewById(R.id.edit_phone);
		edit_break = (TextView) findViewById(R.id.edit_break);
		
		image_item_top = (LinearLayout)findViewById(R.id.image_item_top);
		image_item_bottom = (LinearLayout)findViewById(R.id.image_item_bottom);
		p1 = (ImageView)findViewById(R.id.p1);
		p2 = (ImageView)findViewById(R.id.p2);
		p3 = (ImageView)findViewById(R.id.p3);
		p4 = (ImageView)findViewById(R.id.p4);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.btn_refresh:
			// 提交维修结果 确认维修完毕
			commitReceive();
			break;
		default:
			break;
		}
	}

	private void commitReceive() {
		RepairApi api = new RepairApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {
			
			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
				JSONObject jsonEntity = apiResult.getJsonEntity();
				Log.e("aa", jsonEntity.toString());
				if (jsonEntity != null) {
					// {"d":[{"__type":"DiaryReturn:r","c":"aaa","t":"01:59:05"}],"t":1}

					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							JSONObject obj = jsonEntity.getJSONObject("d");
							String result = obj.getString("r");
							if("1".equals(result)) {
								Utils.showToast(ServicesReceiverActivity.this, "确认成功");
								finish();
							}else {
								Utils.showToast(ServicesReceiverActivity.this, "确认失败");
							}
							break;
						case 1:// 数组


							break;
						case -1:// 错误
							break;

						default:
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
			
			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(ID_LOADING);
			}
			
			@Override
			public <T> void onReturnFailResult(int requestCode, ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
			}
		});
		api.confirmReceive(1,SettingLoader.getGuardNum(this),oddNum);
		
	}
	
	
}
